#工程绝对路径
project_path=$(cd `dirname $0`; pwd)

#工程名
project_name=packagewithscript

#打包模式 Debug/Release
development_mode=Release

#scheme名
scheme_name=packagewithscript

#plist文件所在路径
exportOptionsPlistPath=AppStoreExportOptions.plist

#打包文件夹
packagePath=${project_path}/package

#导出.ipa文件所在路径
exportFilePath=${packagePath}/appstore

#编译路径
buildPath=${packagePath}/build

#发布模式选择
ipaType=1
if [ -n "$1" ] && [ "$1" == "appstore" ]; then
    ipaType=1
elif [ -n "$1" ] && [ "$1" == "adhoc" ]; then
    ipaType=2
else
    echo "请输入你要发布的方式 ? [ 1:app-store 2:ad-hoc ]"
    read ipaType
    while([[ $ipaType != 1 ]] && [[ $ipaType != 2 ]])
    do
        echo "笨蛋，只能输入 1 or 2"
        echo "请输入你想法发布的方式 ? [ 1:app-store 2:ad-hoc ]"
        read ipaType
    done
fi

if [ $ipaType == 1 ]; then
    development_mode=Release
    exportOptionsPlistPath=AppStoreExportOptions.plist
    exportFilePath=${packagePath}/appstore
else
    development_mode=Debug
    exportOptionsPlistPath=AdHocExportOptions.plist
    exportFilePath=${packagePath}/adhoc
fi

#发布平台选择
platform=1
if [ -n "$2" ] && [ "$2" == "appstore" ]; then
    platform=1
elif [ -n "$2" ] && [ "$2" == "fir" ]; then
    platform=2
elif [ -n "$2" ] && [ "$2" == "pgy" ]; then
    platform=3
else
    echo "请输入你要发布的平台 ? [ 1:app-store 2:fir 3:蒲公英 ]"
    read platform
    while([[ $platform != 1 ]] && [[ $platform != 2 ]] && [[ $platform != 3 ]])
    do
        echo "笨蛋，只能输入 1 or 2 or 3"
        echo "请输入你要发布的平台 ? [ 1:app-store 2:fir 3:蒲公英 ]"
        read platform
    done
fi

#操作回滚
rollbackIfNeed(){
	if [ $? -ne 0 ]; then
		if [ -d ${packagePath} ]; then
		    rm -rf ${packagePath} -r
		fi
	fi
	echo '///-----------'
	#这里必须用双引号，要不然打印错误
	echo "/// $1 "
	echo '///-----------'
	echo ''
    exit 1
}

echo '///-----------'
echo '/// 正在清理工程'
echo '///-----------'
echo ''

if [ ! -d ${packagePath} ]; then
	#创建package
	mkdir -p ${packagePath};
else
	#删除ipa
	if [ -d ${exportFilePath} ]; then
		rm -rf ${exportFilePath} -r
	fi
fi
xcodebuild \
clean -configuration ${development_mode} -quiet || rollbackIfNeed '清理失败' 

echo '///-----------'
echo '/// 清理完成'
echo '///-----------'
echo ''

echo '///-----------'
echo '/// 正在编译工程:'${development_mode}
echo '///-----------'
echo ''

xcodebuild \
archive -project ${project_name}.xcodeproj \
-scheme ${scheme_name} \
-configuration ${development_mode} \
-archivePath ${buildPath}/${project_name}.xcarchive -quiet || rollbackIfNeed '编译失败'

echo '///-----------'
echo '/// 编译完成'
echo '///-----------'
echo ''

echo '///-----------'
echo '/// 开始ipa打包'
echo '///-----------'
echo ''
xcodebuild -exportArchive -archivePath ${buildPath}/${project_name}.xcarchive \
-configuration ${development_mode} \
-exportPath ${exportFilePath} \
-exportOptionsPlist ${exportOptionsPlistPath} \
-quiet || rollbackIfNeed '打包失败'
#删除build
if [ -d ${buildPath} ]; then
	rm -rf ${buildPath} -r
fi
if [ ! -e $exportFilePath/$scheme_name.ipa ]; then
	rollbackIfNeed 'ipa包导出失败'
fi
echo '///-----------'
echo '/// 打包ipa完成 '
echo '///-----------'
echo ''

echo '///-------------'
echo '/// 开始发布ipa包 '
echo '///-------------'
echo ''

if [ $platform == 1 ]; then
    #验证并上传到App Store
	xcrun altool --validate-app -f ${exportFilePath}/${scheme_name}.ipa -t ios -u changqing198@126.com -p uhel-yedm-vlnc-hill --output-format xml || rollbackIfNeed 'ipa校验失败'
    xcrun altool --upload-app   -f ${exportFilePath}/${scheme_name}.ipa -t ios -u changqing198@126.com -p uhel-yedm-vlnc-hill --output-format xml || rollbackIfNeed 'ipa上传失败'

    echo '///-------------'
    echo '/// 发布ipa包至AppStore完成'
    echo '///-------------'
    echo ''
elif [ $platform == 2 ]; then
    #上传到Fir
    # 将XXX替换成自己的Fir平台的token
    fir login -T 88aaac07d94035a16aac532574252ab2 || rollbackIfNeed '登录fir失败'
    fir publish $exportFilePath/$scheme_name.ipa || rollbackIfNeed '发布ipa包至fir失败'

    echo '///-------------'
    echo '/// 发布ipa包至fir完成'
    echo '///-------------'
    echo ''
elif [ $platform == 3 ]; then
    #上传到蒲公英
    #蒲公英aipKey
    MY_PGY_API_K=b2cf65fa52e8d8124b70536a10a5230a
    #蒲公英uKey
    MY_PGY_UK=f99a636bcf795c77c17ab5cc4e9bc13d
    curl -F "file=@${exportFilePath}/${scheme_name}.ipa" \
	-F "uKey=${MY_PGY_UK}" \
	-F "_api_key=${MY_PGY_API_K}" \
	https://www.pgyer.com/apiv1/app/upload || rollbackIfNeed '发布ipa包至pgy失败'
	
	echo ''
	echo ''
	echo '///-------------'
    echo '/// 发布ipa包至pgy完成'
    echo '///-------------'
    echo ''
fi

echo "已运行完毕>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>"
exit 0
